package com.soft.security;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.AbstractAuthenticationTargetUrlRequestHandler;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import com.soft.admin.service.impl.cache.HomeServiceCache;

public class XyLogoutHandler extends AbstractAuthenticationTargetUrlRequestHandler implements LogoutSuccessHandler{
	private HomeServiceCache homeServiceCache;
	
	@Override
	public void onLogoutSuccess(HttpServletRequest request,
			HttpServletResponse response, Authentication authentication)
			throws IOException, ServletException {
		UserDetails userDetail = (UserDetails)authentication.getPrincipal();
		this.homeServiceCache.removeUserCache(userDetail.getUsername());
		this.homeServiceCache.deleteMenuCache(userDetail.getUsername());
		response.sendRedirect("../admin/login.jsp");
	}

	@Autowired
	public void setHomeServiceCache(HomeServiceCache homeServiceCache) {
		this.homeServiceCache = homeServiceCache;
	}
}
